﻿using System;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 用户操作日志
    /// </summary>
    public partial class UserOwnerOperateLogUserControl : BaseUserControl
    {
        private string _userId;
        public UserOwnerOperateLogUserControl()
        {
            InitializeComponent();
            if (!WinFormHelper.IsDesignMode())
            {
                gridControl.ModelType = typeof(SysOperateLog);
                filterStartDate.ConfigDate();
                filterEndDate.ConfigDate();
                Mutex.Begin(() => App.ContainerData.SetModel(layoutGroupFilter, SysOperateLogFilter.Default));
            }
        }

        /// <summary>
        /// 加载首页数据
        /// </summary>
        private Task LoadFirstPageAsync()
        {
            return Mutex.EndAsync(() => gridControl.LoadFirstPageAsync());
        }

        /// <summary>
        /// 用户详情传值
        /// </summary>
        /// <param name="userId">用户主键</param>
        public Task LoadAsync(string userId)
        {
            _userId = userId;
            Mutex.Begin(() => App.ContainerData.SetModel(layoutGroupFilter, SysOperateLogFilter.Default));
            return LoadFirstPageAsync();
        }

        private async void btnSearch_Click(object sender, EventArgs e)
        {
            await LoadFirstPageAsync();
        }

        private void GridControl_DetectFilter(object sender, DetectFilterEventArgs e)
        {
            e.Filter = App.ContainerData.GetModel(layoutGroupFilter, new SysOperateLogFilter { OperateUserId = _userId });
        }

        private void GridControl_LoadData(object sender, LoadDataEventArgs e)
        {
            e.Task = async () => await SysLogService.Instance.SelectOperateLogPageListAsync(e.Filter as SysOperateLogFilter);
        }

        private void GridControl_RowDoubleClick(object sender, EventArgs e)
        {
            // new OperateLogDetails(gridControl).ShowDialogOk();
        }
    }
}